package com.xliee.modules.problem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xliee.common.utils.DateUtils;
import com.xliee.modules.problem.entity.ProblemEntity;
import com.xliee.modules.problem.mapper.ProblemMapper;
import com.xliee.modules.problem.service.IProblemService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xliee.modules.sys.entity.UserEntity;
import com.xliee.modules.vehicle.entity.VehicleEntity;
import com.xliee.modules.vehicle.service.IVehicleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lisongsong@xliee.com
 * @since 2024-09-05 10:19:00
 */
@Service
public class ProblemServiceImpl extends ServiceImpl<ProblemMapper, ProblemEntity> implements IProblemService {


    @Autowired
    IVehicleService vehicleService;

    /**
     *  获取首页问题列表
     * @return
     */
    @Override
    public List<ProblemEntity> getIndexList() {
        List<ProblemEntity> list = baseMapper.selectList(new QueryWrapper<ProblemEntity>().orderByDesc("create_time").last("limit 6"));
        return list;
    }

    /**
     * 快速添加日常问题
     */
    @Override
    public void doAddQuickly(ProblemEntity problem) {
        UserEntity newUser = new UserEntity();
        newUser.setUserName(problem.getImei());
        VehicleEntity vehicle = vehicleService.selectByUser(newUser);
        if (vehicle != null) {
            problem.setTitle(vehicle.getOwner()+problem.getType());
            problem.setOwner(vehicle.getOwner());
            problem.setPhone(vehicle.getPhone());
            problem.setOpenTime(DateUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
        }
        this.save(problem);
    }

}
